package com.uxhuanche.ui.widgets.dialog;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.uxhuanche.ui.R;


/**
 * @author ketan
 */
public class CommonDialog extends BaseDialog implements View.OnClickListener {

    private View.OnClickListener vListener;
    private View.OnClickListener mCloseListener;
    private View.OnClickListener vCancelListener;
    protected TextView tvTitle;
    protected TextView tvContent;

    protected Button btnOK;
    protected Button btnCancel;

    protected View middleLine;
    protected ImageButton ibClose;


    private String mOKStr;
    private String mCancelStr;
    private String mTitleStr;
    private String mContentStr;
    private int mBtTextColorRes;
    private int mBtColorRes;
    private int mOkBtBackgroundRes;
    private int mCancelBtBackgroundRes;
    private boolean mSingleStyle = false;
    private int gravity;


    private OnActivityResultListener mOnActivityResultListener;

    public interface OnActivityResultListener {
        void onActivityResult(int requestCode, int resultCode, Intent data);
    }

    public void setOnActivityResultListener(OnActivityResultListener listener) {
        this.mOnActivityResultListener = listener;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_custom_define, null);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        tvTitle = (TextView) getView().findViewById(R.id.tvTitle);
        tvContent = (TextView) getView().findViewById(R.id.tvContent);
        btnOK = (Button) getView().findViewById(R.id.btnOK);
        btnCancel = (Button) getView().findViewById(R.id.btnCancle);
        ibClose = (ImageButton) getView().findViewById(R.id.ibClose);
        middleLine = getView().findViewById(R.id.middleLine);
        if (mSingleStyle) {
            middleLine.setVisibility(View.GONE);
            btnCancel.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(mTitleStr)) {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(mTitleStr);
        } else {
            tvTitle.setVisibility(View.GONE);
            tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
        }

        if (mCloseListener != null) {
            ibClose.setVisibility(View.VISIBLE);
            ibClose.setOnClickListener(mCloseListener);
        }

        tvContent.setText(mContentStr);
        tvContent.measure(0,0);
        tvContent.setMinHeight(tvContent.getHeight());

        btnCancel.setOnClickListener(this);
        btnOK.setOnClickListener(this);
        if (!TextUtils.isEmpty(mOKStr)) {
            btnOK.setText(mOKStr);
        }
        if (!TextUtils.isEmpty(mCancelStr)) {
            btnCancel.setText(mCancelStr);
        }
        if (mBtTextColorRes != 0) {
            btnOK.setTextColor(getResources().getColor(mBtTextColorRes));
            btnCancel.setTextColor(getResources().getColor(mBtTextColorRes));
        }
        if (mBtColorRes != 0) {
            btnOK.setBackgroundColor(getResources().getColor(mBtColorRes));
            btnCancel.setBackgroundColor(getResources().getColor(mBtColorRes));
        }
        if (mOkBtBackgroundRes != 0) {
            btnOK.setBackgroundResource(mOkBtBackgroundRes);
        }
        if (mCancelBtBackgroundRes != 0) {
            btnCancel.setBackgroundResource(mCancelBtBackgroundRes);
        }

        if (gravity != 0) {
            tvContent.setGravity(gravity);
        }
        setOnOutAndBackCancel(outCancleFlag, backCancleFlag);
    }

    @Override
    public void setOnOutAndBackCancel(Boolean outCancelFlag, Boolean backCancelFlag) {
        super.setOnOutAndBackCancel(outCancelFlag, backCancelFlag);
        this.outCancleFlag = outCancelFlag;
        this.backCancleFlag = backCancelFlag;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnOK) {
            if (vListener != null) {
                vListener.onClick(v);
            }
        } else if (id == R.id.btnCancle && vCancelListener != null) {
            vCancelListener.onClick(v);
        }
    }


    public void setInfo(String title, String content) {
        this.mTitleStr = title;
        this.mContentStr = content;
    }

    public void setContentGravity(int gravity) {
        this.gravity = gravity;
    }

    public void singleButton(String text, View.OnClickListener listener) {
        this.mOKStr = text;
        mSingleStyle = true;
        this.vListener = listener;
    }

    public void setBtTextColor(int color) {
        this.mBtTextColorRes = color;
    }

    public void setOKBtBackgroundRes(int resId) {
        this.mOkBtBackgroundRes = resId;
    }

    public void setCancelBtBackgroundRes(int resId) {
        this.mCancelBtBackgroundRes = resId;
    }


    public void setBtColor(int color) {
        this.mBtColorRes = color;
    }


    public void doubleButton(String ok, View.OnClickListener okListener, String cancel, View.OnClickListener cancelListener) {
        this.mOKStr = ok;
        this.mCancelStr = cancel;
        this.vListener = okListener;
        this.vCancelListener = cancelListener;
    }

    public void doubleButton(int okRes, View.OnClickListener okListener, int cancelRes, View.OnClickListener cancelListener) {
        this.mOKStr = getString(okRes);
        this.mCancelStr = getString(cancelRes);
        this.vListener = okListener;
        this.vCancelListener = cancelListener;
    }


    public void setClickListener(View.OnClickListener listener) {
        this.vListener = listener;
    }


    public void setCloseIvListener(View.OnClickListener listener) {
        this.mCloseListener = listener;
    }

    public static void close(CommonDialog dial) {
        if (dial != null && !dial.isRemoving()) {
            try {
                dial.dismissAllowingStateLoss();
            } catch (Exception ex) {
                //nothing
            }
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (mOnActivityResultListener != null) {
            this.mOnActivityResultListener.onActivityResult(requestCode, resultCode, data);
        }
    }
}